import java.util.Arrays;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:数组的定义与使用
 * User: 阿宾
 * Date: 2024-05-04
 * Time: 22:40
 */
public class Test01 {
    public static void main(String[] args) {
        int[] arr1 = {11,22};
        System.out.println(arr1);//[I@0x776ec8df

        int[] arr2 = arr1;
        System.out.println(arr2);

        System.out.println(arr1[0]);
        arr1[1] = 10;
        System.out.println(arr1[1]);
        System.out.println(arr2[1]);
    }
    public static void main18(String[] args) {
        int[] arr1 = new int[2];//默认值为0
        System.out.println(arr1);//打印数组arr1在内存中的地址 [I@0x776ec8df

        int[] arr2 = {1,2,3,4};
        System.out.println(arr2);//打印数组arr2在内存中的地址 [I@0x4eec7777
    }
    public static void main17(String[] args) {
        //1.定义数组存储1~5
        int[] arr = {1, 2, 3, 4, 5};
        //2.循环遍历数组，从0索引开始打乱数据的顺序
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            //生成一个随机索引
            int randomIndex = r.nextInt(arr.length);
            //拿着随机索引指向的元素 跟 i 指向的元素进行交换
            int temp = arr[i];
            arr[i] = arr[randomIndex];
            arr[randomIndex] = temp;
        }
        //当循环结束之后，那么数组中所有的数据已经打乱顺序了
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
    public static void main16(String[] args) {
        int[] arr = {1,2,3,4,5};
        /*System.out.println(arr);*/
        //数组名.fori
        /*for (int i = 0; i < arr.length; i++) {

        }*/
        char[] arr3 = new char[3];
        for (int i = 0; i < arr3.length; i++) {
            System.out.print(arr3[i]);
        }
        System.out.println(1);
        double[] arr2 = {1.1,2.2,3.3};
        /*System.out.println(arr2);*/
    }







    
    //冒泡排序
    public static void bubbleSort(int[] arr){
        for (int i = 0; i < arr.length - 1; i++) {//趟数
            boolean flag = true;
            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j] >= arr[j+1]){
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                    flag = false;
                }
            }
            //一趟下来不发生交换说明已经有序
            if(flag){
                break;
            }
        }
    }
    public static void main15(String[] args) {
        int[] arr = new int[]{1,21,87,-5,14,34};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));

    }

    public static void main14(String[] args) {
        //比较两个数组下标位置元素是否相同
        int[] arr1 = {1,2,3,4,19};
        int[] arr2 = {1,3,3,4,19};
        boolean ret = Arrays.equals(arr1,arr2);//返回值类型是boolean
        System.out.println(ret);
        //填充指定数据到数组中
        int[] arr3 = new int[10];
        Arrays.fill(arr3,5);
        System.out.println(Arrays.toString(arr3));
        //填充指定范围数据
        int[] arr4 = new int[10];
        Arrays.fill(arr4,3,9,-4);
        System.out.println(Arrays.toString(arr4));
    }

    //二分查找
    public static int binarySearch(int[] arr,int key){
        int left = 0;
        int right = arr.length-1;
        while(left <= right){
            int mid = (left + right) / 2;
            if(key > arr[mid]){
                left = mid + 1;
            } else if (key < arr[mid]) {
                right = mid - 1;
            }else {
                return mid;
            }
        }
        return -1;//表示没有找到
    }
    public static void main13(String[] args) {
        int[] arr = {1,183,12,89,25,134};
        Arrays.sort(arr);//将数组从小到大排好
        System.out.println(Arrays.toString(arr));//输出数组元素
        System.out.println(Arrays.binarySearch(arr,25));
    }

    //给定一个数组, 再给定一个元素, 找出该元素在数组中的位置.
    public static int findVal(int[] arr,int key){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == key){
                return i;
            }
        }
        return -1;//表示没有找到
    }
    public static void main12(String[] args) {
        int[] arr = {1,3,5,7,9};
        int ret = findVal(arr,10);
        if(ret != -1){
            System.out.println("找到了，下标是：" + ret);
        }else {
            System.out.println("没有找到");
        }
    }
    //给定一个整型数组, 求平均值
    public static double avg(int[] arr){
        double sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum / arr.length;
    }
    public static void main11(String[] args) {
        //给定一个整型数组, 求平均值
        int[] arr = new int[]{1,2,3,4,5};
        System.out.println(avg(arr));
    }

    public static void main10(String[] args) {
        int[] arr = {1,2,3,4,5};
        //利用数组类进行拷贝
        int[] copy = Arrays.copyOf(arr,arr.length * 2);
        //利用数组类将数组输出
        System.out.println(Arrays.toString(copy));
        //其中的from,to表示数组下标，范围是[from,to)
        int[] copy1 = Arrays.copyOfRange(arr,0,3);
        System.out.println(Arrays.toString(copy1));

        //利用System.arraycopy方法
//        int[] arr = {1,2,3,4,5};
        int[] copy2 = new int[arr.length * 2];
        //将数组arr从下标为1的位置开始拷贝到copy2数组的下标位置为2开始的位置,拷贝的长度是4
        System.arraycopy(arr,1,copy2,2,arr.length-1);
        System.out.println(Arrays.toString(copy2));//[0, 0, 2, 3, 4, 5, 0, 0, 0, 0]
    }

    public static void main9(String[] args) {
        int[] arr = {1,2,3,4,5};
        //拷贝数组元素到另一个数组中
        int[] copy = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            copy[i] = arr[i];
        }
        System.out.println(Arrays.toString(copy));//遍历输出数组copy的元素

        //情况1，不是拷贝
        int[] arr1 = {1,2,3,4,5};
        int[] arr2 = arr1;
        System.out.println(Arrays.toString(arr2));
    }

    public static String myToString(int[] arr){
        String ret = "[";
        for (int i = 0; i < arr.length; i++) {
            ret += arr[i];
            if(i < arr.length-1){
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }
    public static void main8(String[] args) {
        int[] array = {1,2,3,4,5};
        String ret = myToString(array);
        System.out.println(ret);
    }
    public static int[] func3(){
        /*int[] arr = {1,2,3,4};
        return arr;*/
        return new int[]{1,2,3,4};
    }
    public static void main7(String[] args) {
        int[] ret = func3();
        String s = Arrays.toString(ret);
        System.out.println(s);
    }
    public static void func1(int[] arr){
        arr = new int[10];
    }
    public static void func2(int[] arr){
        arr[0] = 19;
    }
    public static void main6(String[] args) {
        int[] arr1 = {1,2,3,4};
        func1(arr1);
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + " ");
        }
        System.out.println();
        int[] arr2 = {1,2,3,4};
        func2(arr2);
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + " ");
        }
    }
    public static void main5(String[] args) {
        int[] arr = new int[]{1,2,3,4,5};
        int[] arr1 = arr;
        System.out.println(arr1[0]);
        arr1[1] = 10;
        System.out.println(arr[1]);

    }
    public static void main1(String[] args) {
        int[] arr1 = {1,2,3,4};
        int[] arr2 = new int[10];//定义一个大小为10的整型数组，初始值都为0
        int[] arr3 = new int[]{1,2,3,4,5,6,7};//赋初始值，会根据定义的值来判断数组的大小
        char[] arr4 = new char[5];
        System.out.println(arr4.length);//打印数组长度

        //数组打印方式
        //for each 循环
        //打印arr3数组
        for (int i = 0; i < arr3.length; i++) {
            System.out.print(arr3[i]+" ");
        }
        System.out.println();
        for (int x:arr3) {
            System.out.print(x+" ");
        }
        System.out.println();

    }

    public static void main2(String[] args) {
        int[] arr1 = new int[10];//创建一个可以存储10个int类型元素的数组
        arr1[0] = 1;
        arr1[1] = 3;
        System.out.println(arr1[0]);
        System.out.println(arr1[1]);
        System.out.println(arr1[2]);//int类型数组默认值是0

    }

    public static void main3(String[] args) {
        //遍历数组

        int[] arr1 = {1,2,3,4,5,6};
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + " ");
        }
    }

    public static void main4(String[] args) {
        int[] arr = new int[]{1,2,3,4,56,10};
        for (int x:arr) {
            System.out.print(x + " ");
        }
        System.out.println();
        String[] arr1 = new String[]{"hello","world","xaioming","zhangsan"};
        for (String y: arr1) {
            System.out.print(y + " ");
        }
    }
}
